using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms.Design;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
using System.Collections;

using System.Windows.Forms;
using System.Drawing;
using Camalon.Drawing;
using Camalon.Drawing.Shapes;

namespace Camalon.Drawing.Design
{
	/// <summary>
	/// Summary description for BezierDesigner.
	/// </summary>
	public class BezierDesigner:VectorShapeDesigner
	{
		public override ShapeAdornment CreateAdornment(ISelectionService selectionService)
		{
			if( this.Component is Bezier)
			return new BezierAdornment((Bezier)this.Component, selectionService);

			return base.CreateAdornment( selectionService);
		}

		protected override string TextVerbAdd
		{
			get{ return "Add Bezier";}
		}

		protected override string TextVerbInsert
		{
			get{ return "Insert Bezier";}
		}

		protected override string TextVerbRemove
		{
			get{ return "Remove Bezier";}
		}

		protected override void OnAddPoint(VectorShape shape, PropertyDescriptor propPoints, AnchorPoint point)
		{
			try
			{
				AnchorPoint[] points = shape.Points;
				ArrayList list = new ArrayList( points );

				int dx = point.X - points[ points.Length-1].X ;dx/=3;
				int dy = point.Y - points[ points.Length-1].Y ;dy/=3;
				AnchorPoint p2 = new AnchorPoint( point.X - dx, point.Y - dy);
				AnchorPoint p1 = new AnchorPoint( p2.X - dx, p2.Y - dy);

				list.AddRange(new AnchorPoint[]{ p1,p2,  point });

				points = new AnchorPoint[list.Count];
				list.CopyTo( points);

				propPoints.SetValue(shape, points);
			}
			catch( Exception e)
			{
				FileWatcher.WriteLine("BezierDesigner::AddPoint\n" + e.Message );
				throw e;
			}

		}

		protected override void OnInsertPoint(VectorShape shape, PropertyDescriptor propPoints, AnchorPoint point, int insertAt)
		{
				AnchorPoint[] points = shape.Points;
				ArrayList list = new ArrayList( points );

				int dx = point.X - points[ insertAt - 1].X ;dx/=3;
				int dy = point.Y - points[ insertAt - 1].Y ;dy/=3;
				AnchorPoint p2 = new AnchorPoint( point.X - dx, point.Y - dy);
				AnchorPoint p1 = new AnchorPoint( p2.X - dx, p2.Y - dy);

				list.InsertRange(insertAt, new AnchorPoint[]{ p1,p2,  point });

				points = new AnchorPoint[list.Count];
				list.CopyTo( points );

				propPoints.SetValue(shape, points);

		}

		protected override void OnRemovePoint(VectorShape shape, PropertyDescriptor propPoints, int removeAt)
		{
			AnchorPoint[] points = shape.Points;
			ArrayList list = new ArrayList( points );

			if( removeAt == list.Count - 1 ) removeAt -=2;
			else if( removeAt != 0) removeAt -= 1;

			list.RemoveRange(removeAt, 3);

			points = new AnchorPoint[list.Count];
			list.CopyTo( points);

			propPoints.SetValue(shape, points);
		}



	}
}
